import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class SimpleCalculator {

    private JFrame frame;
    private JTextField textField;
    private JButton[][] numberButtons = new JButton[4][4];
    private JButton operationButtons = new JButton("=");
    private JPanel panel;

    private double firstNumber;
    private double secondNumber;
    private String lastOperation;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            SimpleCalculator calculator = new SimpleCalculator();
            calculator.frame.setVisible(true);
        });
    }

    public SimpleCalculator() {
        initialize();
    }

    private void initialize() {
        frame = new JFrame("Simple Calculator");
        frame.setBounds(100, 100, 300, 200);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        textField = new JTextField();
        textField.setBounds(6, 6, 288, 56);
        frame.getContentPane().add(textField);
        textField.setColumns(10);

        operationButtons.setBounds(6, 70, 86, 56);
        operationButtons.setText("=");
        operationButtons.setForeground(Color.RED);
        frame.getContentPane().add(operationButtons);

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                numberButtons[i][j] = new JButton();
                numberButtons[i][j].setText(String.valueOf(i + 1));
                numberButtons[i][j].setBounds(118 + i * 52, 34 + j * 52, 52, 52);
                frame.getContentPane().add(numberButtons[i][j]);
            }
        }

        ActionListener actionListener = e -> {
            JButton button = (JButton) e.getSource();
            String input = button.getText();

            if (operationButtons.getText().equals("=")) {
                secondNumber = Double.parseDouble(textField.getText());
                calculate();
            } else {
                firstNumber = Double.parseDouble(textField.getText());
                textField.setText(input);
                lastOperation = null;
            }
        };

        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 4; j++) {
                numberButtons[i][j].addActionListener(actionListener);
            }
        }

        operationButtons.addActionListener(actionListener);
    }

    private void calculate() {
        double result = 0;

        switch (lastOperation) {
            case "+":
                result = firstNumber + secondNumber;
                break;
            case "-":
                result = firstNumber - secondNumber;
                break;
            case "*":
                result = firstNumber * secondNumber;
                break;
            case "/":
                result = firstNumber / secondNumber;
                break;
        }

        textField.setText(String.valueOf(result));
        lastOperation = null;
    }
}